package com.youlian.cloud.spring.config;

import com.youlian.cloud.dao.BasicBaseDao;
import com.youlian.cloud.spring.DaoScannerConfigurer;
import com.youlian.cloud.sql.ConfigurableFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * 持久层配置
 * 这里为配置文件的环境配置,这里需要关注几个点:
 * jdbc.config.path : jdbc的sql文件的存放目录
 * jdbc.config.scan : 需要扫描接口的包路径
 *
 * @author Liukx
 * @create 2018-04-28 11:54
 * @email liukx@elab-plus.com
 **/
@Configuration
public class JdbcBeanConfig {

    @Bean
    public JdbcTemplate jdbcTemplate(@Autowired DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    @Bean
    public BasicBaseDao basicBaseDao(@Autowired JdbcTemplate jdbcTemplate) {
        BasicBaseDao basicBaseDao = new BasicBaseDao();
        basicBaseDao.setJdbcTemplate(jdbcTemplate);
        return basicBaseDao;
    }

    @Bean
    public ConfigurableFactory getConfigurableFactory(@Autowired Environment env) {
        ConfigurableFactory configurableFactory = new ConfigurableFactory();
        configurableFactory.setSqlConfigurableLocations(env.getProperty("jdbc.config.path", "sql"));
        return configurableFactory;
    }

    @Bean
    public DaoScannerConfigurer daoScannerConfigurer(@Autowired ConfigurableFactory configurableFactory, @Autowired Environment env) {
        DaoScannerConfigurer daoScannerConfigurer = new DaoScannerConfigurer();
        daoScannerConfigurer.setBasePackage(env.getProperty("jdbc.config.scan", "com.youlian.**"));
        daoScannerConfigurer.setBasicBaseDaoName("basicBaseDao");
        daoScannerConfigurer.setConfigurableFactory(configurableFactory);
        return daoScannerConfigurer;
    }


}
